package org.aseclipse.osgi.container;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aseclipse.osgi.servlet.BridgeServlet;
import org.eclipse.equinox.http.servlet.HttpServiceServlet;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleListener;
import org.osgi.framework.ServiceRegistration;
import org.osgi.util.tracker.ServiceTracker;

public class ASBundleActivator implements BundleActivator {
	
	private static final Log log_ = LogFactory.getLog(ASBundleActivator.class);
	private BundleListener listener;
	private ServiceRegistration reg;
	ServiceTracker httpServiceTracker;
	// OSGi bridge
	private HttpServiceServlet httpServiceServlet;
	public void start(BundleContext context) throws Exception {
		log_.info(">>> Start ASBundleActivator...");
		listener = new ASBundleListener();
		context.addBundleListener(listener);
		reg = context.registerService(IASBundleManager.class.getName(), ASBundleManager.getInstance(), null);
		httpServiceTracker = new HttpServiceTracker(context);
		httpServiceTracker.open();
		// register to outside frameworkRunner
        httpServiceServlet = new HttpServiceServlet();
        BridgeServlet.registerServletDelegate(httpServiceServlet);
	}

	public void stop(BundleContext context) throws Exception {
		context.removeBundleListener(listener);
		listener = null;
		reg.unregister();
		reg = null;
		httpServiceTracker.close();
		httpServiceTracker = null;
		// unregister from outside frameworkRunner
        BridgeServlet.unregisterServletDelegate(httpServiceServlet);
        httpServiceServlet = null;
		log_.info("<<< Stop ASBundleActivator...");
	}

}
